package com.upcode.weighing.util.ops;

import org.apache.commons.lang3.StringUtils;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 脱敏工具类
 */
public class DesUtil {

    //名字脱敏
    public static String replaceNameX(String str){
        if(!OtherUtills.checkStr(str)){
            return "未填写";
        }
        str=str.trim();
        String reg = ".{1}";
        StringBuffer sb = new StringBuffer();
        Pattern p = Pattern.compile(reg);
        Matcher m = p.matcher(str);
        int i = 0;
        while(m.find()){
            i++;
            if(i==1)
                continue;
            m.appendReplacement(sb, "*");
        }
        m.appendTail(sb);
        return sb.toString();
    }

    //身份证号脱敏
    public static String desensitizedPhoneNumber(String phoneNumber){
        phoneNumber=phoneNumber.trim();
        if(OtherUtills.checkStr(phoneNumber)){
            phoneNumber = phoneNumber.replaceAll("(\\w{3})\\w*(\\w{4})", "$1****$2");
        }else{
            phoneNumber="未填写";
        }
        return phoneNumber;
    }

    //手机号脱敏
    public static String mobileEncrypt(String mobile){
        if(!OtherUtills.checkStr(mobile)){
            return "未填写";
        }
        if(mobile.trim().length()!=11){
            return mobile;
        }
        return mobile.replaceAll("(\\d{3})\\d{4}(\\d{4})","$1****$2");
    }

}
